import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pc = require('picocolors');

import { Log4jsLogger } from './libs/log4js/';

const ENV = process.env.NODE_ENV;

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    origin: true,
    credentials: true,
    maxAge: 1728000,
  });

  app.useLogger(app.get(Log4jsLogger));

  const configService = app.get(ConfigService);
  const port = configService.get('port');
  console.log(
    pc.green(
      `服务端口 :>> PORT: ${pc.red(port)}, NODE_ENV:${pc.red(
        process.env.APP_ENV,
      )}`,
    ),
  );

  if (ENV !== 'prod') {
    const options = new DocumentBuilder()
      .setTitle('API')
      .setDescription('API description')
      .setVersion('1.0')
      .build();
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('api', app, document);
  }

  await app.listen(port);
}
bootstrap();
